Passed
Push — development ( 2f9cbb...cdc56b )
by Peter
09:24 queued 15s
created

ShowMap.tsx ➔ ShowMap   C

Complexity

Conditions 9

Size

Total Lines 92
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 11.2532

Importance

Changes 0
Metric Value
cc 9
eloc 75
dl 0
loc 92
ccs 23
cts 33
cp 0.6969
crap 11.2532
rs 5.4848
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import Map from '../../components/Map';
2
import { useParams } from "react-router-dom";
3
import axios from 'axios';
4
import { API_URL} from '../../helpers/config';
5
import { useEffect, useState, useRef } from 'react';
6
import { Scooter,  Zone } from '../../helpers/map/leaflet-types'
7
import { Label, ToggleSwitch } from 'flowbite-react';
8
import AdminGate from '../../components/AdminGate';
9
10
export default function ShowMap() {
11 1
    const { city }  = useParams();
12 1
    const [zoneData, setZoneData] = useState<Zone[]>([]);
13 1
    const [scooterData, setScooterData] = useState<Scooter[]>([]);
14 1
    const [realTime, setRealTime] = useState(false);
15 1
    const timerRef = useRef<null | number>(null);
16 1
    const [trigger, setTrigger] = useState(0);
17
  
18 1
    const updateRealTime = () => {
19
      setRealTime(!realTime);
20 2
      if (realTime)
21
      {
22
        stopTimer();
23
      } else {
24
        startTimer();
25
      }
26
      
27
    }
28
29 1
    const startTimer = () => {
30 2
      if (!timerRef.current)
31
        {
32
          timerRef.current = setInterval(() => {
33
            setTrigger((prev) => prev + 1);
34
          }, 1000);
35
        }
36
  };
37
38 1
  const stopTimer = () => {
39 2
      if (timerRef.current)
40
        {
41
          clearInterval(timerRef.current);
42
          timerRef.current = null;
43
        }
44
  };
45
    
46 1
    useEffect(() => {
47 1
      const fetchScooters = async() => {
48 1
      try {
49 1
              const response = await axios.get(`${API_URL}/bike/city/${city}`);
50
              setScooterData(response.data);
51
          }
52
          catch(error)
53
          {
54
          }
55
    }
56 1
    fetchScooters();
57
    },[city, trigger])
58
59 1
    useEffect(() => {
60 1
      const fetchZones = async() => {
61 1
      try {
62
63 1
              const response = await axios.get(`${API_URL}/zone/city/${city}`);
64
              setZoneData(response.data);
65
          }
66
          catch(error)
67
          {
68
          }
69
    }
70 1
    fetchZones();
71
    },[city, trigger])
72
73
74 1
  return (
75
    <>
76
    <AdminGate/>
77
      <div data-testid="show-map"><Map city={city ?? "Göteborg"} zoneData={zoneData} scooterData={scooterData}/></div>
78
      <Label htmlFor="realtimetoggle">Vill du uppdatera kartan i realtid?</Label>
79
      <ToggleSwitch id="realtimetoggle" checked={realTime} onChange={updateRealTime}>Uppdatera i realtid?</ToggleSwitch>
80
      <div id="scooter-list" className="mt-4 bg-gray-600 rounded">
81
      <h2 className="text-xl font-bold mb-2">Cyklar i {city}:</h2>
82
      {scooterData.length > 0 ? (
83
          <ul className="list-disc pl-6 list-none">
84
              {scooterData.map((scooter) => (
85
                  <li key={scooter.id} className="mb-2">
86
                      <div className="mt-4 p-6 mx-auto w-1/2 hover:opacity-5 bg-gray-400 rounded text-center">
87
                      <h2><span className="font-semibold">ID:</span> {scooter.id} -{" "}</h2>
88
                      <span className="font-semibold">Batteri:</span> {scooter.batteryLevel}% -{" "}
89
                      <span className="font-semibold">Status:</span> {scooter.status} -{" "}
90
                      <span className="font-semibold">Longitud:</span> {scooter.longitude} -{" "}
91
                      <span className="font-semibold">Latitud:</span> {scooter.latitude}
92
                      </div>
93
                  </li>
94
              ))}
95
          </ul>
96
      ) : (
97
          <p>Inga cyklar tillgängliga i denna stad.</p>
98
      )}
99
    </div>
100
  </>
101
  )
102
};
103